package com.aode.register.view.activity;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.aode.common.bean.request.LoginData;
import com.aode.common.bean.response.User;
import com.aode.common.bean.web.Result;
import com.aode.register.R;
import com.aode.register.databinding.ActivityRegisterBinding;
import com.aode.register.viewmodel.RegisterViewModel;
import com.aode.utils.main.ToastUtils;

public class RegisterActivity extends AppCompatActivity {
    private ActivityRegisterBinding registerBinding;
    private RegisterViewModel registerViewModel;
    private String name;
    private String password;
    private LoginData loginData = new LoginData();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        registerBinding = DataBindingUtil.setContentView(this, R.layout.activity_register);
        registerViewModel = ViewModelProviders.of(this).get(RegisterViewModel.class);
        registerBinding.setRegister(RegisterActivity.this);
        initListener();
    }

    private void initListener() {
        registerViewModel.getRegisterLiveData().observe(this, userResult -> {

        });
    }

    public void click(View view){
        if (view.getId() == R.id.buttonSignUp) {
            boolean a = checkRegister();
            register(a);
        }
    }

    private void register(boolean a) {
        if(a){
            registerViewModel.register(loginData);
        }
    }

    private boolean checkRegister() {
        name = registerBinding.inputName.getText().toString().trim();
        password = registerBinding.inputPassword.getText().toString().trim();
        if(name.equals("")){
            ToastUtils.show(getApplicationContext(),"用户名不能为空");
            return false;
        }else if(password.equals("")){
            ToastUtils.show(getApplicationContext(),"密码不能为空");
            return false;
        }
        return true;
    }

}
